package com.gaowl.gulimall.ware.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaowl.common.utils.PageUtils;
import com.gaowl.common.utils.Query;

import com.gaowl.gulimall.ware.dao.PurchaseDetailDao;
import com.gaowl.gulimall.ware.entity.PurchaseDetailEntity;
import com.gaowl.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            queryWrapper.and(w->{
                w.like("id", key).or().like("purchase_id", key).or().like("sku_id", key)
                        .or().like("sku_num", key).or().like("sku_price", key)
                        .or().like("ware_id", key).or().like("status", key);
            });
        }

        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)){
            queryWrapper.eq("status", status);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id", wareId);
        }


        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params), queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据采购单id查询到关联的采购需求们
     * @param purchaseId 采购单id
     */
    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long purchaseId) {
        List<PurchaseDetailEntity> purchaseDetailEntities = this.baseMapper.selectList(
                new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", purchaseId)
        );
        return purchaseDetailEntities;
    }

}